<template>
  <!-- 首页框架 -->
  <el-container class="layout">
    <!-- 动态绑定样式切换: -->
    <el-aside :width="isOpen?'200px':'64px'">
      <!-- 动态绑定class -->
      <div class="logo" :class="{logo_mini:!isOpen}"></div>
      <!-- 导航菜单,动态激活菜单 -->
      <el-menu
        :default-active="$route.path"
        background-color="#002233"
        text-color="#fff"
        active-text-color="#ffd04b"
        style="border-right:none"
        :collapse="!isOpen"
        :collapse-transition="false"
        router
      >
        <el-menu-item index="/welcome">
          <i class="el-icon-s-home"></i>
          <span slot="title">首页</span>
        </el-menu-item>
        <el-menu-item index="/article">
          <i class="el-icon-document"></i>
          <span slot="title">内容管理</span>
        </el-menu-item>
        <el-menu-item index="/image">
          <i class="el-icon-picture"></i>
          <span slot="title">素材管理</span>
        </el-menu-item>
        <el-menu-item index="/publish">
          <i class="el-icon-s-promotion"></i>
          <span slot="title">发布文章</span>
        </el-menu-item>
        <el-menu-item index="/comment">
          <i class="el-icon-s-comment"></i>
          <span slot="title">评论管理</span>
        </el-menu-item>
        <el-menu-item index="/fans">
          <i class="el-icon-s-custom"></i>
          <span slot="title">粉丝管理</span>
        </el-menu-item>
        <el-menu-item index="/setting">
          <i class="el-icon-setting"></i>
          <span slot="title">个人设置</span>
        </el-menu-item>
      </el-menu>
    </el-aside>
    <el-container>
      <el-header>
        <span
          @click="toggleAside()"
          class="icon"
          :class="{'el-icon-s-fold':isOpen,'el-icon-s-unfold':!isOpen}"
        ></span>
        <span class="text">传智播客</span>
        <!-- 下拉菜单 -->
        <el-dropdown class="my-dropdown" @command="clickItem">
          <span>
            <img class="head" :src="userInfo.photo" alt />
            <span class="name">{{userInfo.name}}</span>
            <i class="el-icon-arrow-down"></i>
          </span>
          <el-dropdown-menu slot="dropdown">
            <!-- 给组件绑定click事件需要组件支持 -->
            <!-- 组件会解析成HTML结构,让解析后的根元素支持原生事件 -->
            <!-- 事件修饰符@click.native -->
            <el-dropdown-item command="setting" icon="el-icon-setting">个人设置</el-dropdown-item>
            <el-dropdown-item command="logout" icon="el-icon-unlock">退出登录</el-dropdown-item>
          </el-dropdown-menu>
        </el-dropdown>
      </el-header>
      <el-main>
        <!-- 二级路由对应组件显示的位置 -->
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
import auth from "@/utils/auth";
// 引入事件总线
import eventBus from "@/eventBus";
export default {
  name: "layout",
  // 数据驱动视图
  data() {
    return {
      // 侧边栏默认展开状态
      isOpen: true,
      // 用户信息
      userInfo: {
        photo: "",
        name: ""
      }
    };
  },
  // 页面初始化时获取用户信息
  created() {
    const { photo, name } = auth.getUser();
    this.userInfo = { photo, name };
    // 接收setting组件中的用户信息修改数据
    eventBus.$on("updateUser", data => {
      this.userInfo.name = data;
    });
    // 接收setting组件中的用户头像修改数据
    eventBus.$on("updatePhoto", data => {
      this.userInfo.photo = data;
    });
  },
  methods: {
    toggleAside() {
      this.isOpen = !this.isOpen;
    },
    // 个人设置
    setting() {
      this.$router.push("/setting");
    },
    // 退出登录
    logout() {
      // 删除用户信息
      auth.delUser();
      // 返回到登录页面
      this.$router.push("/login");
    },
    // 下拉菜单选项
    clickItem(command) {
      this[command]();
    }
  }
};
</script>

<style lang="less" scoped>
.layout {
  width: 100%;
  height: 100%;
  position: absolute;
  top: 0;
  left: 0;
  .el-aside {
    background: #002233;
    .logo {
      width: 100%;
      height: 60px;
      background: #002244 url(../assets/logo_admin.png) no-repeat center/140px
        auto;
    }
    // 样式切换覆盖
    .logo_mini {
      background-image: url(../assets/logo_admin_01.png);
      background-size: 36px auto;
    }
  }
  .el-header {
    border-bottom: 1px solid #ddd;
    line-height: 60px;
    .icon {
      font-size: 24px;
      vertical-align: middle;
    }
    .text {
      vertical-align: middle;
      margin-left: 10px;
    }
    .my-dropdown {
      float: right;
      .head {
        width: 30px;
        height: 30px;
        vertical-align: middle;
      }
      .name {
        font-weight: 700;
        color: #333;
        vertical-align: middle;
        margin: 0 10px;
      }
    }
  }
}
</style>